//
//  LocationService.swift
//  WeatherTools
//
//  Created by 刘飞龙 on 2020/8/31.
//  Copyright © 2020 finals. All rights reserved.
//

import Foundation
import CoreLocation

class LocationService: NSObject, CLLocationManagerDelegate {

    private var locationManager: CLLocationManager? = nil

    func updateLocation() {
        if self.locationManager == nil {
            self.locationManager = CLLocationManager()
            self.locationManager?.delegate = self
            self.locationManager?.desiredAccuracy = kCLLocationAccuracyBest
        }
        let status = CLLocationManager.authorizationStatus()
        if status == CLAuthorizationStatus.notDetermined {
            self.locationManager?.requestAlwaysAuthorization()
        } else {
            self.locationManager?.requestLocation()
        }
    }

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
//        if status == CLAuthorizationStatus.authorized {
//            self.locationManager?.startUpdatingLocation()
//        }
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        print(locations as Any)
    }

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print(error as Any)
    }
}
